package com.itheima.advantagesup.service.impl;

import com.itheima.advantagesup.entity.SysMenu;
import com.itheima.advantagesup.mapper.MenuMapper;
import com.itheima.advantagesup.service.MenuService;
import com.itheima.advantagesup.vo.MenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper mapper;

    @Override
    public List<MenuVO> allMenu() {
        List<SysMenu> allMenv = mapper.Menu(0);
        List<MenuVO> allMenvs = new ArrayList<>();
        allMenv.forEach(sysMenu -> allMenvs.add(sysMenu.getMenv()));

        allMenvs.forEach(m -> {
            List<SysMenu> menu = mapper.Menu(m.getId());
            List<MenuVO> allMenv1 = new ArrayList<>();
            menu.forEach(s -> {
                allMenv1.add(s.getMenv());
            });

            allMenv1.forEach(e -> {
                List<SysMenu> menuVOS = mapper.Menu(e.getId());
                List<MenuVO> allMenv2 = new ArrayList<>();
                menuVOS.forEach(menues->{
                    allMenv2.add(menues.getMenv());
                });
                e.setChildren(allMenv2);
            });
            m.setChildren(allMenv1);
        });
        return allMenvs;
    }

    @Override
    public void addMenu(MenuVO Menu) {
        System.out.println(Menu);
        mapper.addMenu(Menu);
    }

    @Override
    public void delMenu(String id) {
        mapper.delMenu(id);
    }

    @Override
    public MenuVO rowInfoMenu(String id) {
        MenuVO menuVOS = mapper.rowInfoMenu(id);
        return menuVOS;
    }

    @Override
    public void updateMenu(MenuVO menu) {
        mapper.updateMenu(menu);
    }
}
